package com.example.demo.controller;


import com.example.demo.bean.HttpResult;
import com.example.demo.bean.User;
import com.example.demo.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {//实现与web前端的交互
    @Autowired//将service注入web层
    UserService userService;
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }
    @RequestMapping("/index")
    public String index(){
        return "index";
    }
    @RequestMapping("/register")
    public String register(){return "register";}
    @RequestMapping("/login")
    public HttpResult<User> login(@RequestBody User user , HttpServletRequest request){
        HttpResult<User> httpResult = new HttpResult<>();
        HttpSession session = request.getSession();//获取session
        user.setLoginPass(DigestUtils.md5DigestAsHex(user.getLoginPass().getBytes()));
        User u = userService.queryUser(user);
        if(u!=null){
            session.setAttribute("user",u);//将登录成功的信息保存到session中
            httpResult.setData(u);
            return httpResult;
        }else{
            return httpResult;
        }
    }
    @RequestMapping("/test")
    public HttpResult<List<User>> test(String id){
        System.out.println(id);
        HttpResult<List<User>> result = new HttpResult<>();
        List<User> list = new ArrayList<>();
        for(int i=0;i<5;i++){
            User user = new User();
            user.setLoginId("tset" + i);
            list.add(user);
        }
        result.setData(list);
        return result;
    }
    @RequestMapping("/toUserList")
    public HttpResult<List<User>> toUserList(@RequestBody User user){
        HttpResult<List<User>> result = new HttpResult<>();
        List<User> list = userService.queryUserList(user);
        result.setData(list);
        return result;
    }
    @RequestMapping("isLogin")
    //HttpServletRequest 获取提交请求的内容
    public HttpResult<Boolean> isLogin(HttpServletRequest request){
        HttpResult<Boolean> httpResult = new HttpResult<>();
        HttpSession  session = request.getSession();
        User user = (User) session.getAttribute("user");
        if(user == null){
            httpResult.setData(false);
        }else {
            httpResult.setData(true);
        }
        return  httpResult;
    }
    @RequestMapping("/updatePassword")
    public HttpResult<Boolean> updatePassword(String newPass,HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        user.setLoginPass(newPass);
        int i = userService.updateUser(user);
        HttpResult<Boolean> result = new HttpResult<>();
        if(i == 1){
            result.setData(true);
            session.removeAttribute("user");
        }
        return result;
    }
    @RequestMapping("/insert")
    public HttpResult<Boolean> insert(@RequestBody User user,HttpServletRequest request){
        HttpResult<Boolean> result = new HttpResult<>();
        if(!(StringUtils.hasLength(user.getLoginId()))){
            result.setData(false);
        }else {
            user.setLoginPass(DigestUtils.md5DigestAsHex(user.getLoginPass().getBytes()));
            int i = userService.insertUser(user);
            if (i == 1) {
                result.setMsg("注册失败");
                result.setData(true);
            }else {
                result.setMsg("注册成功");
                result.setData(false);
            }
        }
        return result;
    }
    @RequestMapping("/cx")
    public HttpResult<Boolean> cx(@RequestBody User user){
         User user2= userService.queryUser2(user);
        HttpResult<Boolean> result = new HttpResult<>();
        if(user2 != null){
            result.setData(true);
        }else{
            result.setData(false);
        }
        return result;
    }

}
